package com.example.ndk_2_3_2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.ndk_2_3_2.utils.PermissionManager;
import com.yanzhenjie.permission.Permission;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    static {
        System.loadLibrary("exosip");
        System.loadLibrary("native-lib");
    }

    private Button register_btn;
    private Button test_btn;
    private TextView message_tv;

    public native String stringFromJNI();

    public native String callTestFromJNI();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        register_btn = findViewById(R.id.register_btn);
        register_btn.setOnClickListener(this);
        message_tv = findViewById(R.id.message_tv);
        message_tv.setText("消息！");
        test_btn=findViewById(R.id.test_btn);
        test_btn.setOnClickListener(this);
        initPermission();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.register_btn:
                stringFromJNI();
                message_tv.setText("点击注册按钮");
                break;
            case R.id.test_btn:
                String testMessage = callTestFromJNI();
                message_tv.setText(testMessage);
                break;

        }
    }

    /**
     * 初始化权限事件
     */
    private void initPermission() {
        //检查权限
        PermissionManager.requestPermission(MainActivity.this, new PermissionManager.Callback() {
            @Override
            public void permissionSuccess() {
                PermissionManager.requestPermission(MainActivity.this, new PermissionManager.Callback() {
                    @Override
                    public void permissionSuccess() {
                        PermissionManager.requestPermission(MainActivity.this, new PermissionManager.Callback() {
                            @Override
                            public void permissionSuccess() {
                                Toast.makeText(MainActivity.this, "授权成功！", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void permissionFailed() {
                                Toast.makeText(MainActivity.this, "授权失败！", Toast.LENGTH_SHORT).show();
                            }
                        }, com.yanzhenjie.permission.Permission.Group.STORAGE);
                    }

                    @Override
                    public void permissionFailed() {
                        Toast.makeText(MainActivity.this, "请求权限失败！", Toast.LENGTH_SHORT).show();
                    }
                }, com.yanzhenjie.permission.Permission.Group.MICROPHONE);
            }

            @Override
            public void permissionFailed() {

            }
        }, Permission.Group.CAMERA);

    }

}
